//
//  YHMeAddressListViewController.m
//  YHApp
//
//  Created by -- on 2021/6/19.
//

#import "HAAddressListVC.h"
#import "HAAddAddressVC.h"
#import "HAAddressMo.h"
#import "HAAddressListCell.h"

@interface HAAddressListVC ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic ,strong) NSMutableArray *dataArray;
/***/
@property (nonatomic, copy) YHChoiceAddressnBlock block;

@end

@implementation HAAddressListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
    
    MJRefreshGifHeader *mj_header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    self.tableView.mj_header = mj_header;
    
    MJRefreshBackGifFooter *mj_footer = [MJRefreshBackGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(laodMoreData)];
    self.tableView.mj_footer = mj_footer;
    
    [self.tableView.mj_header beginRefreshing];
    // Do any additional setup after loading the view.
}


-(void)returnBlock:(YHChoiceAddressnBlock)block {
    _block = block;
}

- (void)initUI{
    self.baseTLabel.text = @"收货地址";
    
    UIButton *addButton = [[UIButton alloc]init];
    [addButton setTitle:@"去新增" forState:UIControlStateNormal];
    [addButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    addButton.titleLabel.font = [UIFont systemFontOfSize:18];
    addButton.backgroundColor = [UIColor colorWithHexString:@"#000000"];
    [addButton addTarget:self action:@selector(addButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addButton];
    [addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(-0);
        make.bottom.mas_equalTo(0);
        make.height.mas_offset(54);
    }];

    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(app_topH+44);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-54);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 128;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WS(weakSelf)
    HAAddressListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YHMeAddressListCellID"];
    if (!cell) {
        cell = [[HAAddressListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YHMeAddressListCellID"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.dataArray[indexPath.row];
    cell.YHMeAddressListCellDelegateBlock = ^(HAAddressMo * _Nonnull model) {
        HATipsPopView *menu = [[HATipsPopView alloc]initWithTitle:@"提示" content:@"确定删除该地址吗?" left:@"取消" right:@"确定"];
        [menu show];
        menu.XZYGTiShiRightBlock = ^{
            [weakSelf addressdel:model];
        };
    };
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    WS(weakSelf)
    if (self.isChoice) {
        if (self.block) {
            self.block(self.dataArray[indexPath.row]);
        }
        [self.navigationController popViewControllerAnimated:YES];
    }else {
        HAAddAddressVC *vc = [[HAAddAddressVC alloc]init];
        vc.model = self.dataArray[indexPath.row];
        [vc returnBlock:^{
            [weakSelf.tableView.mj_header beginRefreshing];
        }];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

-(void)addButtonClick {
    WS(weakSelf)
    HAAddAddressVC *vc = [[HAAddAddressVC alloc]init];
    [vc returnBlock:^{
        [weakSelf.tableView.mj_header beginRefreshing];
    }];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)addressdel:(HAAddressMo *)model {
    
    NSDictionary *dict = @{
        @"ids":model.ID,
    };
    SYRequest *request1 = [[SYRequest alloc]init];
    request1.requestUrl = [NSString stringWithFormat:@"address/del"];
    request1.requestMethod = SYRequestMethodGET;
    request1.parms = dict;

    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request1] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request1) {
            
            [self.tableView.mj_header endRefreshing];
            
            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                [self.dataArray removeObject:model];
                [self.tableView reloadData];
            }else{
                [MBProgressHUD showError:dataModel.msg];
            }
        }
    }];
}


-(void)loadNewData {
    
    NSDictionary *dict = @{
        @"pageSize":@"10",
        @"page":@"1",
    };
    SYRequest *request1 = [[SYRequest alloc]init];
    request1.requestUrl = [NSString stringWithFormat:@"address/lists"];
    request1.requestMethod = SYRequestMethodPOST;
    request1.parms = dict;

    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request1] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request1) {
            
            [self.tableView.mj_header endRefreshing];
            
            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                [self.dataArray removeAllObjects];
                NSArray *arr = [HAAddressMo mj_objectArrayWithKeyValuesArray:dataModel.data];
                [self.dataArray addObjectsFromArray:arr];
            }else{
                [MBProgressHUD showError:dataModel.msg];
            }
            
            [self.tableView reloadData];
            
        }
    }];
    
}

-(void)laodMoreData {
    
    NSInteger count = ceilf(self.dataArray.count*0.1)+1;
    NSString *pageNo = [NSString stringWithFormat:@"%ld",(long)count];
    
    NSDictionary *dict = @{
        @"pageSize":@"10",
        @"page":pageNo,
    };
    SYRequest *request1 = [[SYRequest alloc]init];
    request1.requestUrl = [NSString stringWithFormat:@"address/lists"];
    request1.requestMethod = SYRequestMethodPOST;
    request1.parms = dict;

    [self.tableView.mj_footer beginRefreshing];
    
    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request1] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request1) {
            
            [self.tableView.mj_footer endRefreshing];
            
            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                NSArray *arr = [HAAddressMo mj_objectArrayWithKeyValuesArray:dataModel.data];
                [self.dataArray addObjectsFromArray:arr];
            }else{
                [MBProgressHUD showError:dataModel.msg];
            }
            
            [self.tableView reloadData];
        }
    }];
    
}


- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        // 去掉滚动条
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        // 设置背景颜色
        _tableView.backgroundColor = [UIColor khaBGColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
